<?php

namespace app\common\model\plus\discount;

use app\common\model\BaseModel;
use app\common\model\product\Product;

/**
 * 参与记录模型
 */
class DiscountGitProduct extends BaseModel
{
    protected $name = 'discount_git_product';
    protected $pk = 'gift_product_id';
    //附加字段
    protected $append = [];



    public function file()
    {
        return $this->belongsTo('app\\common\\model\\file\\UploadFile', 'image_id', 'file_id');
    }

    public function product()
    {
        return $this->hasOne(Product::class, 'product_id', 'product_id');
    }



    const FORM_SCENE_ADD = 'add';
    const FORM_SCENE_EDIT = 'edit';

    /**
     * 获取列表数据
     */
    public function getList($state = 10, $search = '', $params)
    {
        $model = $this;
        if ($state) {
            $model = $model->where('state', '=', (int)$state);
        }
        if (!empty($search)) {
            $model = $model->where('product_name', 'like', '%' . $search . '%');
        }
        $model = $model->where('shop_supplier_id', '=', $params['shop_supplier_id']);
        $model = $model->where('is_delete', '=', 0);
        // 查询列表数据
        return $model->order(['create_time' => 'desc']) -> with(['file','product'])->paginate($params);
    }

    /**
     * 查询所有列表数据
     */
    public function getAll()
    {
        // 查询列表数据
        return $this->where('is_delete', '=', '0')->with(['store'])->select();

    }

    /**
     * 新增记录
     */
    public function add($data)
    {
        $data['app_id'] = self::$app_id;

        return self::create($data);
    }

    /**
     * 编辑记录
     */
    public function edit($data)
    {

        return $this->save($data);
    }

    /**
     * 软删除
     */
    public function setDelete()
    {
        return $this->save(['is_delete' => 1]);
    }


    public static function detail($gift_product_id){
       return self::where('gift_product_id',$gift_product_id) -> with(['file']) -> field('*,product_id product') -> find();
    }

    public function getProductAttr($value)
    {


        return (new Product()) -> where('product_id',$value) -> value('product_name');
    }


}